package com.txw.serviceedu.service.impl;

import com.txw.sercicebase.exceptionhandller.LingqiException;
import com.txw.serviceedu.entity.EduChapter;
import com.txw.serviceedu.entity.EduCourse;
import com.txw.serviceedu.entity.EduCourseDescription;
import com.txw.serviceedu.entity.vo.CourseInfoVo;
import com.txw.serviceedu.entity.vo.CoursePublishVo;
import com.txw.serviceedu.mapper.EduCourseMapper;
import com.txw.serviceedu.service.EduChapterService;
import com.txw.serviceedu.service.EduCourseDescriptionService;
import com.txw.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.txw.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author TanXiongWei
 * @since 2021-11-28
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    //添加课程基本信息方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加课程基本信息
        //CourseInfoVo对象装换eduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new LingqiException(20001, "添加课程信息失败");
        }
        String cid = eduCourse.getId();

        //向课程简介表添加课程
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    /**
     * 根据课程查询课程基本信息
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        //查询描述表
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    /**
     * 修改课程基本信息
     *
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int updateById = baseMapper.updateById(eduCourse);
        if (updateById == 0) {
            throw new LingqiException(20001, "修改课程信息失败");
        }

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public void removeCourse(String courseId) {
//        根据课程ID删除小节
        videoService.removeVideoByCourseId(courseId);

        //根据课程ID删除章节
        chapterService.removeChaoterByCourseId(courseId);

        //根据ID删除描述
        eduCourseDescriptionService.removeById(courseId);
        //根据课程ID找到课程本身
        int result = baseMapper.deleteById(courseId);

        if (result == 0) {
            throw new LingqiException(20001, "删除失败");
        }

    }

    // 根据课程id 查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        return baseMapper.getPublishCourseInfo(id);
    }


}
